package myky.com.doctorapp.fragment;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.github.nukc.stateview.Constant;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.umeng.analytics.MobclickAgent;


import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

import kotlin.Deprecated;
import myky.com.doctorapp.R;
import myky.com.doctorapp.adapter.HomeArticalAdapter;
import myky.com.doctorapp.entity.HomeArtiCalEntity;
import myky.com.doctorapp.entity.HealthEntity;
import myky.com.doctorapp.base.BaseFragment;
import myky.com.doctorapp.contract.NewSubFragmentContract;
import myky.com.doctorapp.presenter.NewSubFragmentPresenter;
import myky.com.doctorapp.ui.HealthArticalActivity;
import myky.com.doctorapp.utils.RecyclerUtils;


/**
 * 健康资讯的子Fragment
 * 建议以{@link HomeNewsSubFragment}替代
 */
@Deprecated(message = "此类页面不建议MVP")
@SuppressLint("ValidFragment")
public class NewsSubFragment extends BaseFragment<NewSubFragmentPresenter, List<HealthEntity.PayLoadBean.ArticleListBean>> implements NewSubFragmentContract.View, OnLoadMoreListener, BaseQuickAdapter.OnItemChildClickListener {
    HomeArticalAdapter homeArticalAdapter;
    List<HomeArtiCalEntity> homeArtiCalEntities;
    @BindView(R.id.artcalListView)
    RecyclerView artcalListView;
    @BindView(R.id.loadingLayout)
    ImageView loadingLayout;
    private int mTagId;
    private HashMap<String, String> hasMap = new HashMap();

    boolean canLoadMore;


    public NewsSubFragment(int mTagId) {
        this.mTagId = mTagId;
    }

    @Override
    public NewSubFragmentPresenter getPresenter() {
        return new NewSubFragmentPresenter();
    }


    @Override
    protected int injectTarget() {
        return 0;
    }

    @Override
    protected void doRetry() {
//        mPresenter.getHealthList(String.valueOf(mTagId), RecyclerUtils.getSortByFrist(0, 9));
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void scrollToFirst(Message message) {
        if (message.what == Constant.SCROLLTOFIST) {
            if (homeArtiCalEntities.size() > 0) {
                artcalListView.smoothScrollToPosition(0);
            }
        }
    }

    @Override
    public void unRegisterSomething() {
        super.unRegisterSomething();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_news_sub;
    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);
        homeArtiCalEntities = new ArrayList<>();
        artcalListView.setLayoutManager(new LinearLayoutManager(getActivity()));
        homeArticalAdapter = new HomeArticalAdapter(homeArtiCalEntities);
        artcalListView.setAdapter(homeArticalAdapter);
        homeArticalAdapter.setOnItemChildClickListener(this);
        homeArticalAdapter.setOnLoadMoreListener(() -> {
            if (canLoadMore) {
                mPresenter.getMoreHealthList(String.valueOf(mTagId), RecyclerUtils.getSortByItem(10));
            }
        }, artcalListView);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
//        //获取健康学堂列表第一次查看0-9条信息
        mPresenter.getHealthList(String.valueOf(mTagId), RecyclerUtils.getSortByFrist(0, 9));
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        if (hidden) {
            loadingLayout.setVisibility(View.VISIBLE);
            //获取健康学堂列表第一次查看0-9条信息
        } else {
            loadingLayout.setVisibility(View.VISIBLE);
            mPresenter.getHealthList(String.valueOf(mTagId), RecyclerUtils.getSortByFrist(0, 9));

        }
    }


    @Override
    public void onLoadMore(RefreshLayout refreshLayout) {

        mPresenter.getMoreHealthList(String.valueOf(mTagId), RecyclerUtils.getSortByItem(10));
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        List<HomeArtiCalEntity> data = homeArticalAdapter.getData();
        HomeArtiCalEntity homeArtiCalEntity = data.get(position);
        hasMap.put("Title", homeArtiCalEntity.getTitle());
        MobclickAgent.onEvent(getContext(), "CM_001", hasMap);
        Intent intent = new Intent(getActivity(), HealthArticalActivity.class);
        intent.putExtra("Articleid", homeArtiCalEntity.getArticleid());
        getActivity().startActivity(intent);
    }

    @Override
    public void showDataError(String errorMessage) {

    }

    @Override
    public void showDataSuccess(List<HealthEntity.PayLoadBean.ArticleListBean> datas) {

    }

    @Override
    public void showLoadingView(String msg) {

    }

    @Override
    public void showHealthtList(List<HealthEntity.PayLoadBean.ArticleListBean> healthlist) {

        homeArtiCalEntities.clear();
        final List<HealthEntity.PayLoadBean.ArticleListBean> articleLists = healthlist;
        if (articleLists != null && articleLists.size() > 0) {
            canLoadMore = true;
            homeArticalAdapter.loadMoreComplete();
            int size = articleLists.size();
            HealthEntity.PayLoadBean.ArticleListBean articleListBean = articleLists.get(0);
            HomeArtiCalEntity homeArtiCalEntityImage = new HomeArtiCalEntity();
            homeArtiCalEntityImage.setCreatetime(articleListBean.getCreatetime());
            homeArtiCalEntityImage.setResource(articleListBean.getResource());
            homeArtiCalEntityImage.setTitle(articleListBean.getTitle());
            homeArtiCalEntityImage.setArticleid(articleListBean.getArticleid());
            homeArtiCalEntityImage.setType(HomeArtiCalEntity.IMAGITEM);
            homeArtiCalEntities.add(homeArtiCalEntityImage);
            if (size > 1) {
                for (int i = 1; i < size; i++) {
                    HealthEntity.PayLoadBean.ArticleListBean bean = articleLists.get(i);
                    HomeArtiCalEntity homeArtiCalEntityNormal = new HomeArtiCalEntity();
                    homeArtiCalEntityNormal.setType(HomeArtiCalEntity.NORMALCONTEN);
                    homeArtiCalEntityNormal.setCreatetime(bean.getCreatetime());
                    homeArtiCalEntityNormal.setTitle(bean.getTitle());
                    homeArtiCalEntityNormal.setResource(bean.getResource());
                    homeArtiCalEntityNormal.setArticleid(bean.getArticleid());
                    homeArtiCalEntities.add(homeArtiCalEntityNormal);
                }
            }
            homeArticalAdapter.loadMoreComplete();
        } else {
            homeArticalAdapter.loadMoreComplete();
        }
        loadingLayout.postDelayed(() -> {
            if (homeArtiCalEntities.size() > 0) {
                loadingLayout.setVisibility(View.GONE);
            }
        }, 500);
        if (homeArtiCalEntities.size() > 0) {
            artcalListView.smoothScrollToPosition(0);
        }
        homeArticalAdapter.notifyDataSetChanged();
    }

    @Override
    public void showMoreHealthtList(List<HealthEntity.PayLoadBean.ArticleListBean> healthlist) {
        final List<HealthEntity.PayLoadBean.ArticleListBean> articleLists = healthlist;
        if (articleLists == null) {
            homeArticalAdapter.loadMoreEnd();
        } else {
            int size = articleLists.size();
            for (int i = 0; i < size; i++) {
                HealthEntity.PayLoadBean.ArticleListBean bean = articleLists.get(i);
                HomeArtiCalEntity homeArtiCalEntityNormal = new HomeArtiCalEntity();
                homeArtiCalEntityNormal.setType(HomeArtiCalEntity.NORMALCONTEN);
                homeArtiCalEntityNormal.setCreatetime(bean.getCreatetime());
                homeArtiCalEntityNormal.setTitle(bean.getTitle());
                homeArtiCalEntityNormal.setResource(bean.getResource());
                homeArtiCalEntityNormal.setArticleid(bean.getArticleid());
                homeArtiCalEntities.add(homeArtiCalEntityNormal);
            }
            homeArticalAdapter.loadMoreComplete();
        }
        homeArticalAdapter.notifyDataSetChanged();
    }

}
